<?php	namespace Home\Controller;
/**
 * 我的购物车管理控制器
 * @author 武斌   <wubin.mail@foxmail.com>
 */
class MyCartController extends CommonController
{
	
	/**
	 * 首页
	 */
	public function index()
	{
		if(isset($_SESSION['cart'])){
			$cart= $_SESSION['cart'];
			View::with('cart',$cart['total']);
			View::with('goods',$cart['goods']);			
		}	
		if(IS_POST)
		{
			$this->login();
		}	
		//载入购物车模板
		View::make(TEMPLATE . 'mycart.html');
	}
	/**
	 * 异步添加购物车
	 */
	public function ajaxAddCart()
	{
		if(!IS_AJAX) return false;
		//加入购物车商品数量
		$num = Q('post.num',0,'intaval');
		$spec = explode(',', rtrim(Q('post.spec'),','));
		$color = $spec[0];//所选商品颜色
		$size = $spec[1];//所选商品数量		
		//商品id
		$gid=$_SESSION['gid'];
		//单一商品数据
		$goods = Db::table('goods')->where("gid",$gid)->first();
		//商品数量
		$data = array(
			'id'	=>$gid,//商品id
			'name'=>$goods['gname'],
			'num' => $num,
			 'price' => $goods['shopprice'], 
			 'shangpin'=>$goods,
			  'options' => array(
			  		'color' => $color, 
			  		 'size' => $size,
			  		 'pic'=>$goods['pic']
			 )		
		);
		Cart::add($data);
		View::ajax($_SESSION['cart']);	
	}
	/**
	 * 异步修改购物车
	 */
	public function edit()
	{
		if(!IS_AJAX) $this->error('非法请求');
		$sid = Q('post.sid');
		$shuliang = Q('post.shuliang',0,'intaval');
		$data=array(
		'sid'=>$sid,// 唯一 sid，添加购物车时自动生成 
		'num'=>$shuliang
		); 
		Cart::update($data);
		$total = $_SESSION['cart']['total'];
		View::ajax($total);		
	}
	 /**
	  * 全选异步得到商品总价
	  */
	  public function allCheched(){
	  	$data = $_SESSION['cart']['total'];
	  	View::ajax($data);
	  }
	/**
	 * 异步删除一条购物车一条数据
	 */	
	public function delOneCart()
	{
		if(!IS_AJAX) $this->error('非法请求');
	 	$sid = Q('post.sid',0,'intaval');//购物车商品sid
		$xiaoji = Q('post.xiaoji',0,'intaval');//当前商品的小计
		$shuliang = Q('post.shuliang',0,'intaval');//当前商品数量
		unset($_SESSION['cart']['goods'][$sid]);//删除该条记录
		$_SESSION['cart']['total_rows']=$_SESSION['cart']['total_rows']-$shuliang;//把session里边总条数修改
		$_SESSION['cart']['total']=$_SESSION['cart']['total']-$xiaoji;//修改session里边总价格
		$total = $_SESSION['cart']['total'];
		View::ajax($total);		
	}
	/**
	 * 清空购物车
	 */
	public function delALLCart()
	{
		unset($_SESSION['cart']);
		View::success('清空完成',U('index'));	
	}
	/**
	 * 立即购买
	 */
	public function ajaxGoBuy()
	{
		if(!IS_AJAX) return false;
		$num = Q('post.num',0,'intaval');
		$spec = explode(',', rtrim(Q('post.spec'),','));
		$color = $spec[0];//所选商品颜色
		$size = $spec[1];//所选商品数量			
		$gid=$_SESSION['gid'];//商品id
		//单一商品数据
		$goods = Db::table('goods')->where("gid",$gid)->first();	
			//商品数量
			$data = array(
				'id'	=>$gid,//商品id
				'name'=>$goods['gname'],
				'num' => $num,
				 'price' => $goods['shopprice'], 
				 'shangpin'=>$goods,
				  'options' => array(
				  		'color' => $color, 
				  		 'size' => $size,
				  		 'pic'=>$goods['pic']
				 )		
			);	
		Cart::add($data);
		View::ajax(1);					
	}
	/**
	 * 登陆
	 */
	public function login()
	{
		//实例化User模型
		$userModel = new \Home\Model\User;
		//执行user模型里边的login方法
		if(!$userInfo = $userModel->login()){
			//提示错误信息
			$this->error($userModel->getError());
		}
		//账号锁定时相应提示信息	
		if($userInfo['is_lock'] == 1){
			$this->error('您的账号已被锁定,请与管理员联系');
		}	
		$_SESSION['uid']=$userInfo['uid'];
		$_SESSION['username']=$userInfo['username'];
		View::success('登陆成功',U('index'));				
	}
	
}